package dper.study.codes.activemq;

/**
 * Created by chenlei on 2017/7/2.
 */
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


import dper.study.codes.common.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;


/**
 * ActiveMQ的消息队列模式
 * Description:
 * 队列消息的生产者
 *
 */
@Component("queueSender")
public class QueueSender {

    /**
     * 通过Qualifier来注入对应的Bean
     */
    @Autowired
    @Qualifier("jmsQueueTemplate")
    private JmsTemplate jmsTemplate;

    /**
     * Description:
     * 发送消息到指定的队列(目标)
     *
     * @param queueName 队列名称
     * @param message   消息内容
     */
    public void send(String queueName, final String message) {
        jmsTemplate.send(queueName, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(message);
            }
        });
    }

    public static void main(String[] argv){

        QueueSender queueSender = SpringContextUtils.getBeanByType(QueueSender.class) ;
        queueSender.send(QueueConstants.FIRSTTEST_QUEUE,"hekkki");
        QueueReceiver1 queueReceiver = SpringContextUtils.getBeanByType(QueueReceiver1.class) ;
    }
}
